﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;
using System.ComponentModel;
using System.Data.SqlClient;


namespace WinLogicUniversity
{
    public partial class frmManageItem : Form
    {
        LogicUniversityT2Entities entity = new LogicUniversityT2Entities();

        public frmManageItem()
        {
            InitializeComponent();
        }

        private void frmManageItem_Load(object sender, EventArgs e)
        {
            bindgridview();
            txtItemNo.Text = DBBroker.ItemBroker.AutoGenItemID();
            var SupplierData = entity.Suppliers;
            cboSupplierName.DataSource = SupplierData;
            cboSupplierName.DisplayMember = "SupplierName";
            var CategoryData = entity.Categories;
            cboCategoryName.DataSource = CategoryData;
            cboCategoryName.DisplayMember = "CategoryName";
            ClearControls();
            btnAdd.Text = "Add";
        }

        #region BindtoGridView
        private void bindgridview()
        {
            dgItem.DataSource = DBBroker.ItemBroker.getItemList();
            dgItem.Columns[3].Visible = false;
            dgItem.Columns[4].Visible = false;
            dgItem.Columns[5].Visible = false;
            dgItem.Columns[6].Visible = false;
            dgItem.Columns[7].Visible = false;
            dgItem.Columns[8].Visible = false;
            dgItem.Columns[9].Visible = false;
            dgItem.Columns[10].Visible = false;
            dgItem.Columns[11].Visible = false;
            dgItem.Columns[12].Visible = false;
            dgItem.Columns[13].Visible = false;
        }
        #endregion

       

        #region clear
        void ClearControls()
        {
            txtItemNo.Text = DBBroker.ItemBroker.AutoGenItemID();
            cboSupplierName.Text = "Choose Supplier Name";
            cboPriority.Text = "Choose Priority";
            cboCategoryName.Text = "Choose Category Name";
            txtDescription.Text = "";
            txtBinNo.Text = DBBroker.ItemBroker.AutoGenBinNo();
            cboUOM.Text = "Choose UOM";
            txtReorderLevel.Text = "";
            txtReorderQty.Text = "";
            txtPrice.Text = "";
            

        }

        #endregion

        #region Validation
        private Boolean MyValidation()
        {
            errorProvider1.Clear();
            if (cboSupplierName.Text.Trim() == "" || cboSupplierName.Text == "Choose Supplier Name")
            {
                errorProvider1.SetError(cboSupplierName, "Please Choose Supplier Name");
                return false;
            }

            else if (cboPriority.Text.Trim() == "" || cboPriority.Text == "Choose Priority")
            {
                errorProvider1.SetError(cboPriority, "Please Choose Priority");
                return false;
            }
            else if (cboCategoryName.Text.Trim() == "" || cboCategoryName.Text == "Choose Category Name")
            {
                errorProvider1.SetError(cboCategoryName, "Please Type Category Name");
                return false;
            }
            else if (cboCategoryName.Text.Trim() == "" || cboCategoryName.Text == "Choose Category Name")
            {
                errorProvider1.SetError(cboCategoryName, "Please Type Category Name");
                return false;
            }
            else if (txtDescription.Text.Trim() == "")
            {
                errorProvider1.SetError(txtDescription, "Please Type Description");
                return false;
            }
            //else if (txtBinNo.Text.Trim() == "")
            //{
            //    errorProvider1.SetError(txtBinNo, "Please Type Bin No");
            //    return false;
            //}
            else if (cboUOM.Text.Trim() == "Choose UOM")
            {
                errorProvider1.SetError(cboUOM, "Please Choose Unit of Measure");
                return false;
            }
            else if (txtReorderLevel.Text.Trim() == "" || DBBroker.Validation.IsNaturalNumber(txtReorderLevel.Text)==false || txtReorderLevel.Text == "0")
            {
                errorProvider1.SetError(txtReorderLevel, "Please Type Valid Reorder Level");
                return false;
            }
            else if (txtReorderQty.Text.Trim() == "" || DBBroker.Validation.IsNaturalNumber(txtReorderQty.Text) == false || txtReorderQty.Text == "0")
            {
                errorProvider1.SetError(txtReorderQty, "Please Type Valid Reorder Quantity");
                return false;
            }
            else if (txtPrice.Text.Trim() == "")
            {
                errorProvider1.SetError(txtPrice, "Please Type Price");
                return false;
            }
           
            else
            {
                return true;
            }
        }
        #endregion

        public void BindtoEdit()
        {
            if (dgItem.CurrentRow != null)
            {
                txtItemNo.Text = dgItem.CurrentRow.Cells[0].Value.ToString();
                txtDescription.Text = dgItem.CurrentRow.Cells[1].Value.ToString();
                cboSupplierName.Text = dgItem.CurrentRow.Cells[4].Value.ToString();
                if (dgItem.CurrentRow.Cells[11].Value.ToString() == dgItem.CurrentRow.Cells[4].Value.ToString())
                {
                    //cboPriority.SelectedIndex = -1;
                    cboPriority.Text = "First";
                }
                else if (dgItem.CurrentRow.Cells[12].Value.ToString() == dgItem.CurrentRow.Cells[4].Value.ToString())
                {
                    //cboPriority.SelectedIndex = 0;
                    cboPriority.Text = "Second";
                }
                else if (dgItem.CurrentRow.Cells[13].Value.ToString() == dgItem.CurrentRow.Cells[4].Value.ToString())
                {
                    //cboPriority.SelectedIndex = 1;
                    cboPriority.Text = "Third";
                }
                cboCategoryName.Text = dgItem.CurrentRow.Cells[5].Value.ToString();
                txtBinNo.Text = dgItem.CurrentRow.Cells[6].Value.ToString();
                cboUOM.Text = dgItem.CurrentRow.Cells[7].Value.ToString();
                txtReorderLevel.Text = dgItem.CurrentRow.Cells[8].Value.ToString();
                txtReorderQty.Text = dgItem.CurrentRow.Cells[9].Value.ToString();
                txtPrice.Text = dgItem.CurrentRow.Cells[2].Value.ToString();
                

            }
        }

        private void btnCategoryAdd_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmCategory cat = new frmCategory();            
            cat.ShowDialog();
            
        }

        private void dgItem_DoubleClick(object sender, EventArgs e)
        {
            if (dgItem.RowCount == 0)
            {
                MyMessageBox.ShowBox("There is no information");
            }
            else
            {
                btnAdd.Text = "Update";
                BindtoEdit();
                cboCategoryName.Enabled = false;
                cboUOM.Enabled = false;
                errorProvider1.Clear();
            }
        }

        private void AddtoDetail()
        {
            if (btnAdd.Text == "Update")
            {
                //int intCount = Convert.ToInt32(entity.ItemDetails.Count(g => g.ItemID));
                int count = (from p in entity.ItemDetails where p.ItemID == txtItemNo.Text select p).Count();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            {

                Boolean myvalidate;
                myvalidate = MyValidation();

                try
                {
                    if (myvalidate == true)
                    {

                        Item item = new Item();
                        ItemDetail itemdetail = new ItemDetail();

                        if (btnAdd.Text == "Add")
                        {

                            item.ItemID = txtItemNo.Text;
                            item.CategoryID = lblCategoryID.Text;
                            item.ItemDescription = txtDescription.Text;
                            item.BinNo = txtBinNo.Text;

                            if (cboPriority.Text == "First")
                            {
                                item.FirstSupplier = cboSupplierName.Text;
                                item.SecondSupplier = "Null";
                                item.ThirdSupplier = "Null";
                            }
                            if (cboPriority.Text == "Second")
                            {
                                item.SecondSupplier = cboSupplierName.Text;
                                item.FirstSupplier = "Null";
                                item.ThirdSupplier = "Null";
                            }
                            if (cboPriority.Text == "Third")
                            {
                                item.ThirdSupplier = cboSupplierName.Text;
                                item.FirstSupplier = "Null";
                                item.SecondSupplier = "Null";
                            }
                            item.ReorderLevel = Convert.ToInt32(txtReorderLevel.Text);
                            item.ReorderQuantity = Convert.ToInt32(txtReorderQty.Text);                            
                            item.UOM = cboUOM.Text.ToString();
                            itemdetail.ItemID = txtItemNo.Text;
                            itemdetail.SupplierID = lblSupplierID.Text;
                            itemdetail.Price = Convert.ToDecimal(txtPrice.Text);
                            itemdetail.ItemDetailID = DBBroker.ItemBroker.AutoGenItemDetailID();
                            entity.AddToItems(item);
                            entity.AddToItemDetails(itemdetail);
                            entity.SaveChanges();
                            bindgridview();
                            MyMessageBox.ShowBox("Saved Successful!");
                            ClearControls();
                            //errorProvider1.Clear();
                        }
                        else
                        {
                            ItemDetail itemupdate = entity.ItemDetails.First(ite => ite.ItemID == txtItemNo.Text);
                            //ItemDetail itemupdate = new ItemDetail();
                            int count = (from p in entity.ItemDetails where p.ItemID == txtItemNo.Text select p).Count();                            

                            if (count < 3)
                            {
                                //txtItemNo.Text = Convert.ToString(count);
                                if (cboPriority.Text == "First")
                                {
                                    itemupdate.Item.FirstSupplier = cboSupplierName.Text;
                                    //itemupdate.Item.SecondSupplier = "Null";
                                    //itemupdate.Item.ThirdSupplier = "Null";
                                }
                                if (cboPriority.Text == "Second")
                                {
                                    itemupdate.Item.SecondSupplier = cboSupplierName.Text;
                                    //itemupdate.Item.FirstSupplier = "Null";
                                    //itemupdate.Item.ThirdSupplier = "Null";
                                }
                                if (cboPriority.Text == "Third")
                                {
                                    itemupdate.Item.ThirdSupplier = cboSupplierName.Text;
                                    //itemupdate.Item.FirstSupplier = "Null";
                                    //itemupdate.Item.SecondSupplier = "Null";
                                }
                                itemdetail.ItemDetailID = DBBroker.ItemBroker.AutoGenItemDetailID();
                                itemdetail.ItemID = txtItemNo.Text;
                                itemdetail.SupplierID = lblSupplierID.Text;
                                itemdetail.Price = Convert.ToDecimal(txtPrice.Text);
                                entity.AddToItemDetails(itemdetail);
                                entity.SaveChanges();
                                bindgridview();
                                MyMessageBox.ShowBox("Saved Successful!");
                                ClearControls();
                            }
                        }
                     }
                }

                catch (SqlException ex)
                {
                    MyMessageBox.ShowBox("SQL Error " + ex.Number + ":" + ex.Message);
                }
            }
        }

        private void cboCategoryName_SelectedIndexChanged(object sender, EventArgs e)
        {
            Category c = (Category)cboCategoryName.SelectedItem;
            string selectedcatid = c.CategoryID;
            lblCategoryID.Text = selectedcatid.ToString();
        }

        private void cboSupplierName_SelectedIndexChanged(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if (btnAdd.Text == "Add")
            {
                Supplier s = (Supplier)cboSupplierName.SelectedItem;
                string selectedId = s.SupplierID;
                lblSupplierID.Text = selectedId.ToString();
            }
            else
            {
                string a = dgItem.CurrentRow.Cells[11].Value.ToString();
                string b = dgItem.CurrentRow.Cells[12].Value.ToString();
                string c = dgItem.CurrentRow.Cells[13].Value.ToString();
                if (cboSupplierName.Text == a || cboSupplierName.Text == b || cboSupplierName.Text == c)
                {
                    errorProvider1.SetError(cboSupplierName, "Can not be same supplier!");
                }
            }
        }
    }
}
